package com.liyuxiang.handler.security;

import com.alibaba.fastjson.JSON;
import com.liyuxiang.utils.WebUtils;
import com.liyuxiang.vm.ResponseResult;
import com.liyuxiang.vm.enums.ResultStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("授权失败");
        accessDeniedException.printStackTrace();
        WebUtils.renderString(response, JSON.toJSONString(ResponseResult.errorResult(ResultStatusEnum.NO_OPERATOR_AUTH)));
    }
}
